import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
export const database = uni.getStorageSync('database')

// 数据表名称
export const user = 'user'
const deviceInfo = 'deviceInfo'
const deviceType = 'deviceType'
const lease = 'lease'
const money = 'money'
const price = 'price'

const offLineDtoList = 'offLineDtoList'
const inComeDtoList = 'inComeDtoList'
const trunOverDtoList = 'trunOverDtoList'
const passConfig = 'passConfig'
// 用户数据表
/*
customerAccount 唯一账号 userid
nickname 名称
phone 手机
avatar 头像
gender 性别
customerIdNumber 身份证
agentAccount 上级代理商账号
rentNum 租赁次数
overdueNum 逾期次数
lastTimeRent 上次租赁时间
grade 会员等级
*/
// 创建数据表
export const cratetable = async () => {

	let sqltxt = [
		`create table if not exists ${user}("customerAccount" VARCHAR(16),"nickname" VARCHAR(16),"phone" VARCHAR(24),"gender" int(1),"avatar" VARCHAR(128),"customerIdNumber" VARCHAR(24),"agentAccount" VARCHAR(24),"guarantorName" VARCHAR(24),"guarantorId" VARCHAR(24),"guarantorPhone" VARCHAR(24),"guarantorAddress" VARCHAR(24),"customerAddress" VARCHAR(24),"rentNum" int(4),"overdueNum" int(4),"lastTimeRent" VARCHAR(16),"grade" int(1))`,
		`create table if not exists ${deviceInfo}("customerAccount" VARCHAR(24),"productId" VARCHAR(24),"productModel" VARCHAR(24),"productProperty" VARCHAR(64),"battery" int(3),"stockStatus" int(2),"stockReserveAccount" VARCHAR(24),"counter" init(5),"productSecret" VARCHAR(24),"photo" VARCHAR(128))`,
		`create table if not exists ${deviceType}("productModel" VARCHAR(24),"manufacturer" VARCHAR(24),"brand" VARCHAR(24),"modelDesc" VARCHAR(24),"photo" VARCHAR(128),"tenantable" int(8),"reserve" int(8),"inStore" int(8),"leaseRatio" int(3))`,
		`create table if not exists ${price}("productModel" VARCHAR(24),"priceGroup" VARCHAR(128),"agencySecurityDeposit" VARCHAR(8),"priceModel" VARCHAR(128),"commission" VARCHAR(2))`,
		`create table if not exists ${money}
		("customerAccount" VARCHAR(24),
		"onMoney" int(8),
		"offMoney" int(8),
		"amountTotal" int(8),
		"amountIncome" int(8),
		"amountTurnover" int(8),
		"amountDeposit" int(8))`,
		`create table if not exists ${lease}("customerAccount" VARCHAR(16),"nickname" VARCHAR(16),"phone" VARCHAR(24),"avatar" VARCHAR(128),"rentNum" int(4),"overdueNum" int(4),"productModel" VARCHAR(16),"productId" VARCHAR(16),"userAddress" VARCHAR(64),"bondsMan" VARCHAR(16),"bondsManPhone" VARCHAR(24),"remind" int(1),"leaseDay" int(2),"leasePrice" int(4),"expirationTime" VARCHAR(16),"leaseStatus" int(1),"history" VARCHAR(512),"overdueDay" int(2),"leaseId" VARCHAR(16),"nextPopupTime" VARCHAR(16),"lastTimeRent" VARCHAR(16),"photo" VARCHAR(128),"rentCode" VARCHAR(24),"rentDtos" VARCHAR(512),"backDtos" VARCHAR(512),"overTime" VARCHAR(16),"currProp" VARCHAR(512),"counter" init(5),"iSync" init(1),"agencySecurityDeposit" VARCHAR(8))`,
		`create table if not exists ${offLineDtoList}
		("tradeCode" VARCHAR(24),
		"offlineAmountOpe" int(8),
		"onlineAmountAll" int(8),
		"offlineAmountAll" int(8),
		"opeCode" VARCHAR(16),
		"productId" VARCHAR(16),
		"createTime" VARCHAR(24),
		"createDate" VARCHAR(24))`,
		`create table if not exists ${inComeDtoList}
		("tradeCode" VARCHAR(24),
		"productId" VARCHAR(16),
		"opeCode" VARCHAR(16),
		"onlineAmountAll" int(8),
		"offlineAmountAll" int(8),
		"incomeAmountOpe" int(8),
		"incomeAmountAll" int(8),
		"createTime" VARCHAR(24),
		"createDate" VARCHAR(24))`,
		`create table if not exists ${trunOverDtoList}
		("tradeCode" VARCHAR(24),
		"productId" VARCHAR(16),
		"opeCode" VARCHAR(16),
		"onlineAmountAll" int(8),
		"offlineAmountAll" int(8),
		"turnoverAmountOpe" int(8),
		"turnoverAmountAll" int(8),
		"createTime" VARCHAR(24),
		"createDate" VARCHAR(24))`,
		`create table if not exists ${passConfig}("chargeUnit" VARCHAR(1),"counter" VARCHAR(6),"createTime" VARCHAR(24),
		"days" VARCHAR(2),"keycode" VARCHAR(16),"pid" VARCHAR(16),"rentCode" VARCHAR(16),"secKey" VARCHAR(16),"sendStatus" VARCHAR(1),"unlockStatus" VARCHAR(1))`,
	];
	
	return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

}

// 添加用户
export const addUser = async (pushData) => {
	// 数据库中customerAccount 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${user} where customerAccount = '${pushData.customerAccount}'`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')

		let data = setObjSql(pushData)
		let sqltxt = `INSERT  INTO ${user} (${condition}) VALUES(${data})`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	} else { //存在 ，更改

		let data = obj2str_sql(pushData)
		let sqltxt = `update  ${user} set ${data} where customerAccount = '${pushData.customerAccount}'`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	}
}

// 删除用户 customerAccount 
export const removeUser = async (customerAccount) => {
	let sqltxt = [
		`delete from  ${user}  where customerAccount = '${customerAccount}'`
	];
	return sql.executeSQL(uni.getStorageSync('database'), sqltxt);

}
// 通过Id获取用户
export const getUserToId = async (customerAccount) => {
	let sqltxt = `select * from ${user}  where customerAccount = '${customerAccount}'`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

// 获取所有的user
export const getUserAll = async () => {
	let sqltxt = `select * from ${user}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

// 退出登录删除数据库
export const delDBUser = async () => {
	let sqltxt = `delete from ${user}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

export const delDBdeviceInfo = async () => {
	let sqltxt = `delete from ${deviceInfo}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}

export const delDBdeviceType = async () => {
	let sqltxt = `delete  from ${deviceType}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}
export const delDBlease = async () => {
	let sqltxt = `delete  from ${lease}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}
export const delDBmoney = async () => {
	let sqltxt = `delete  from ${money}`;
		return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}
export const delDBprice = async () => {
	let sqltxt = `delete  from ${price}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}
export const delDBpassConfig = async () => {
	let sqltxt = `delete  from ${passConfig}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

}